<template>
	<view>
		<cu-custom  bgColor="bg-black-two"  :isBack="true">
			<block slot="content">{{titleText}}</block>
		</cu-custom>
		<view class="my-bg">
				<view class="flex-wrap">
					<view class="flex  my-title-bg">
						<text style="width: 100px;">发票抬头</text>					
						<input class="uni-input" name="header" placeholder="请填写抬头名称(必填)" v-model="header" />					
					</view>
					<view class="flex  my-title-bg">
						<text style="width: 100px;">纳税人识别号</text>					
						<input style="width: 70%;" class="uni-input" name="tax_id" placeholder="请填写纳税人识别号(必填)" v-model="tax_id" />					
					</view>
					<view class="flex  my-title-bg"  style="border: unset;">
						<text style="width: 100px;">邮箱地址</text>					
						<input class="uni-input" name="email" placeholder="用于接收电子发票(必填)" v-model="email"/>					
					</view>				
				</view>		
			</view>
			<view class="padding flex flex-direction">
				<button class="cu-btn round bg-green-two" style="height: 45px;" @click="getServerData()">保存</button>
			</view>
		</view>
		
</template>

<script>
	//post API
	var _self;
	export default{
		data(){
			return{
				titleText:'',
				// header:"",发票抬头 tax_id:"";纳税人识别号  email:""邮箱地址
				userId:"",header:"",tax_id:"",email:"",
				hasSetBg:false,
				editId:""
			}
		},
		onLoad(option){
			_self = this;
			if (Object.keys(option).length != 0) {
				//修改
				this.editId = option.id;
				this.getEditData(option.id);
				this.titleText = '修改发票抬头';
			} else {
				console.log(this.obj);
				// uni.setNavigationBarTitle({
				// 	title: '新增发票抬头'
				// });
				this.titleText = '添加发票抬头';
			}
		},
		onShow(){
			_self = this;
			// uni.setNavigationBarColor({
			// 	frontColor: this.hasSetBg ?  "#000000" : "#262626",
			// 	backgroundColor: this.hasSetBg ? "#F8F8F8" : "#CCCCCC"
			// })
		},
		methods:{
			//获取修改信息
	async	getEditData(id){
				uni.showLoading({ title: '加载中...', mask: true });
				//获取修改信息
				let [err,res] = await this.$http.get('editInvoice',{
					id:id
				},{
					token:true,
					checkToken:true ,
				});
				// 失败
				if (!this.$http.errorCheck(err,res)){
					uni.hideLoading();
					return false;
				}
				uni.hideLoading();
				this.header = res.data.data.header;
				this.tax_id = res.data.data.tax_id;
				this.email = res.data.data.email;
				console.log(res.data.data);
			},
			
	async	getServerData(){
				uni.showLoading({
					title:'正在提交..',
					mask:true
				})
				//获取当前登录的用户ID
				if(this.editId == null || this.editId == ''){
					//新增
					let [err,res] = await this.$http.post('addInvoice',{
						header:this.header,
						tax_id:this.tax_id,
						email:this.email
					},{
						token:true,
						checkToken:true ,
					});
					// 失败
					if (!this.$http.errorCheck(err,res)){
						uni.hideLoading();
						return false;
					}
					uni.hideLoading();
					uni.showToast({
					    title: '提交成功',
					});								
					setTimeout(function () {
					    uni.hideToast();
						uni.navigateBack({
							delta:1
						})
					}, 2000);
				} else {
					//修改
					let [err,res] = await this.$http.post('editInvoice',{
						id:this.editId,
						header:this.header,
						tax_id:this.tax_id,
						email:this.email
					},{
						token:true,
						checkToken:true ,
					});
					// 失败
					if (!this.$http.errorCheck(err,res)){
						uni.hideLoading();
						return false;
					}
					uni.hideLoading();
					uni.showToast({
						title: '提交成功',
					});								
					setTimeout(function () {
						uni.hideToast();
						/* uni.navigateTo({
							url:'/pages/center/fapiao/fapiao-list'
						}) */
						uni.navigateBack({
							delta:1,//返回层数，2则上上页
						})
					}, 2000);
				}
				
				
			}
		}
	}	
</script>

<style>
	page {
		/* background-color: #E6E6E6; */
	}
	.uni-input-placeholder{
		color: #808080; 
	}
	.uni-textarea-placeholder{
		color: #808080; 
	}
</style>